<template>
  <view class="homeWrap">
    <image
      src="../../static/home-bg.png"
      mode="widthFix"
      style="width: 100vw; position: absolute; top: -32rpx; left: 0;"
    />
    <view style="z-index: 2;">
      <up-search placeholder="请输入产品名称" :disabled="true" :showAction="false" height="68" margin="42rpx 30rpx 60rpx" searchIconSize="34" placeholderColor="#909399; font-size: 24rpx;" @click="goSearch"></up-search>
    </view>
    <view style="z-index: 2;">
      <up-text type="" color="#fff" size="42" text="改变从现在开始 遇见崭新的自己" margin="0 30rpx 60rpx"></up-text>
    </view>
    <view style="z-index: 2; padding: 0 30rpx;">
      <up-swiper :indicator="true" indicatorActiveColor="#79C435" indicatorInactiveColor="#c5dba0" :autoplay="true"
        :list="banner" indicatorMode="dot" :circular="true" height="calc((100vw - 30rpx) * 0.4)" radius="24"
      ></up-swiper>
    </view>
    <view class="moduleWrap" v-for="item in moduleList" :key="item.id">
      <view class="row" style="align-items: center;">
        <view>
          <up-text type="" color="#000" size="48" :bold="true" :text="item.title"></up-text>
        </view>
        <image
          src="../../static/me-leaf-green.png"
          mode="scaleToFill"
          style="height: 15px; width: 20px; margin-left: 8px;"
        />
      </view>
      <view class="categoryBlock">
        <image
          src="../../static/storeSuccessBk.png"
          mode="widthFix"
          class="categoryTopImage"
        />
        <view class="cardTop">
          <view class="cardTopLeftLine"></view>
          <view>
            <up-text type="" color="#fff" size="48" :bold="true" margin="0 30rpx" :text="item.title"></up-text>
          </view>
          <view class="cardToprightLine"></view>
        </view>
        <view class="combinationBlock">
          <view class="row combinationBox">
            <image
              :src="item.banner"
              mode="scaleToFill"
              style="width: 100%; height: 272rpx;"
            />
          </view>
        </view>
        <view class="categoryCardBlock">
          <view class="categoryCard" v-for="product in item.goodsList" :key="product.id" @click="goProductDetail(product.id)">
            <view style="height: 194rpx; width: 194rpx;">
              <u-image height="194" width="194" :src="product.goodsPic" :lazy-load="true" mode="aspectFit"></u-image>
            </view>
            <view class="categoryTitle">
              <up-text type="" color="#30322C" :bold="true" align="center" size="24" :text="product.name" :lines="1"></up-text>
              <view class="categoryPrice">
                <text class="unit">￥</text>
                <text class="price">{{ product.price }}</text>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
    <up-gap height="calc(180rpx + env(safe-area-inset-bottom) / 2)" bgColor="#f2f2f2"></up-gap>
  </view>
  <common-shopCard-modal :isLogin="isLogin"></common-shopCard-modal>
  <custom-tab-bar/>
</template>

<script>
import { store } from '@/store';
import { queryBanner, queryModule } from '@/services/home';
import { setTabBarSelected, setTabBarList } from '@/utils/common';

export default {
  data() {
    return {
      banner: [],
      moduleList: [],
    }
  },
  computed: {
    isLogin() {
      const isLogin = this.$store.state.isLogin;
      setTabBarList(isLogin);
      return isLogin;
    }
  },
  onLoad(option) {
    console.log('option', option);
    const { inviteUserId } = option || {};
    if (inviteUserId) {
      store.commit('changeState', { inviteUserId })
    }
  },
  async onShow() {
    setTabBarSelected(0);
    this.getBanner();
    this.getModule();
  },
  methods: {
    goSearch() {
      uni.navigateTo({
        url: '/pages/search/home'
      })
    },
    getBanner() {
      const _that = this;
      queryBanner().then((res) => {
        console.log('queryBanner', res)
        const { data, success } = res || {};
        if (success) {
          _that.banner = data?.split(',') || [];
        }
      })
    },
    getModule() {
      const _that = this;
      queryModule().then((res) => {
        console.log('queryModule', res)
        const { data, success } = res || {};
        if (success) {
          _that.moduleList = data || [];
        }
      })
    },
    goProductDetail(id) {
      uni.navigateTo({
        url: `/pages/productDetail/index?id=${id}`,
      })
    }
  },
}
</script>

<style lang="scss" scoped>
.homeWrap {
  min-height: 100vh;
  background-color: #f2f2f2;
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100vw;
  overflow-x: hidden;
}

.moduleWrap {
  z-index: 2;
  padding: 0 30rpx;
  margin-top: 60rpx;
}

.categoryBlock {
  width: 100%;
  background-color: #fff;
  align-items: center;
  border-radius: 30rpx;
  box-shadow: 0 12rpx 32rpx 0 rgba(0,0,0,0.05);
  position: relative;
  display: flex;
  flex-direction: column;
  margin-top: 30rpx;
}
.categoryTopImage {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: calc(348 * (100vw - 60rpx) / 690);
  border-top-left-radius: 30rpx;
  border-top-right-radius: 30rpx;
}
.combinationBlock {
  width: calc(100% - 40rpx);
  padding: 20rpx;
  z-index: 2;
}
.combinationBox {
  width: 100%;
  align-items: center;
  background-color: #fff;
  border-radius: 30rpx;
  overflow: hidden;
  box-shadow: 0 12rpx 32rpx 0 rgba(0,0,0,0.05);
}
.categoryCardBlock {
  padding: 32rpx 22rpx;
  display: flex;
  flex-direction: row;
  width: calc(100% - 44rpx);
  justify-content: space-between;
}
.categoryCardBlock::after {
  content: " ";
  width: 30%;
  height: 10rpx;
}
.categoryCard {
  margin: 0 10rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 30%;
}
.categoryTitle {
  height: 64rpx;
  margin-top: 16rpx;
  margin-bottom: 4rpx;
}
.categoryPrice {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: "center";
}
.unit {
  font-size: 16rpx;
  color: #59B235;
}
.price {
  font-size: 32rpx;
  color: #59B235;
}
.cardTop {
  height: calc(170*(100vw - 60rpx)/690 - 24rpx);
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding-top: 24rpx;
  z-index: 2;
}
.cardTopLeftLine {
  height: 4rpx;
  width: 44rpx;
  background: linear-gradient(to right, rgba(255,255,255,0) 0%, #fff 100%);
}
.cardToprightLine {
  height: 4rpx;
  width: 44rpx;
  background: linear-gradient(to left, rgba(255,255,255,0) 0%, #fff 100%);
}
</style>
